package cn.tedu.system.shippingcart.service.impl;

import cn.tedu.system.base.exception.ServiceException;
import cn.tedu.system.base.response.JsonResult;
import cn.tedu.system.base.response.StatusCode;
import cn.tedu.system.shippingcart.mapper.CartMapper;
import cn.tedu.system.order.mapper.OrderMapper;
import cn.tedu.system.shippingcart.pojo.dto.CartAddDto;
import cn.tedu.system.shippingcart.pojo.dto.CartDeleteDto;
import cn.tedu.system.shippingcart.pojo.dto.CartQuery;
import cn.tedu.system.shippingcart.pojo.dto.CartUpdateDto;
import cn.tedu.system.shippingcart.pojo.entity.Cart;
import cn.tedu.system.shippingcart.pojo.vo.CartVO;
import cn.tedu.system.shippingcart.service.CartService;
import cn.tedu.system.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderMapper orderMapper;

    //添加购物车
    @Override
    public void addCart(CartAddDto cartAddDto, HttpSession session) {
        log.debug("cartAddDto:{}",cartAddDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        //查询购物车中是否有商品与要添加的是同一种
        CartQuery cartQuery = new CartQuery();
        cartQuery.setUserId(userVO.getId());
        cartQuery.setProductId(cartAddDto.getProductId());
        Integer num = cartMapper.selProduct(cartQuery);
        if(num!=null && num >0){
            CartUpdateDto cartUpdateDto = new CartUpdateDto();
            cartUpdateDto.setProductId(cartAddDto.getProductId());
            cartUpdateDto.setQuantity(cartAddDto.getQuantity()+num);
            updateCart(cartUpdateDto,session);
        }else {
            //查询商品价格
            Long productId = cartAddDto.getProductId();
            Double price = orderMapper.selectPrice(productId);
            //计算单种商品总价
            Double totalPrice=cartAddDto.getQuantity()*price;

            Cart cart = new Cart();
            BeanUtils.copyProperties(cartAddDto, cart);
            cart.setUserId(userVO.getId());
            cart.setSelected(0);
            cart.setTotalPrice(totalPrice);
            cart.setCreateTime(new Date());
            cart.setUpdateTime(new Date());

            if (cartMapper.addCart(cart)<=0){
                throw new ServiceException(StatusCode.FAILED);
            }
        }
    }

    //移除购物车
    @Override
    public void deleteCart(Long productId, HttpSession session) {
        log.debug("productId:{}",productId);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        CartDeleteDto deleteDto = new CartDeleteDto();
        deleteDto.setUserId(userVO.getId());
        deleteDto.setProductId(productId);

        if (cartMapper.deleteCart(deleteDto)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //修改购物车商品
    @Override
    public void updateCart(CartUpdateDto cartUpdateDto, HttpSession session) {
        log.debug("cartUpdateDto:{}",cartUpdateDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        //查询商品价格
        Double totalPrice=0.0;
        if(cartUpdateDto.getQuantity()!=null) {
            Long productId = cartUpdateDto.getProductId();
            Double price = orderMapper.selectPrice(productId);
            //计算单种商品总价
            totalPrice=cartUpdateDto.getQuantity()*price;
        }

        Cart cart = new Cart();
        BeanUtils.copyProperties(cartUpdateDto, cart);
        cart.setUserId(userVO.getId());
        cart.setTotalPrice(totalPrice);
        cart.setUpdateTime(new Date());

        if (cartMapper.updateCart(cart)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //查询购物车功能
    @Override
    public List<CartVO> selectCart(HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        log.debug("购物车业务参数:{}",userVO.getId());
        List<CartVO> list = cartMapper.selectCart(userVO.getId());
        if (list == null && list.size() == 0){
            throw new ServiceException(StatusCode.FAILED);
        }
        return list;
    }

    //计算选中商品总价
    @Override
    public Double allPrice(HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        Double sum = 0.0;
        List<Double> price = cartMapper.allTotalPrice(userVO.getId());
        for (Double d : price) {
            sum += d;
        }
        return sum;
    }
}

